package me.jessyan.mvparms.demo.mvp.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.home.DaggerHomeComponent;
import me.jessyan.mvparms.demo.di.module.home.HomeModule;
import me.jessyan.mvparms.demo.mvp.contract.home.HomeContract;
import me.jessyan.mvparms.demo.mvp.presenter.home.HomePresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.HomeViewPagerAdapter;
import me.jessyan.mvparms.demo.mvp.ui.fragment.BrandFragment;
import me.jessyan.mvparms.demo.mvp.ui.fragment.LifeFragment;
import me.jessyan.mvparms.demo.mvp.ui.fragment.MineFragment;
import me.jessyan.mvparms.demo.mvp.ui.fragment.OneLevelBaseFragment;
import me.jessyan.mvparms.demo.mvp.ui.fragment.ShoppingCardFragment;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomStatusBar;
import me.jessyan.mvparms.demo.mvp.ui.widget.HomeTabLayout;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/*
* 主页一级页面
 */
public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.View ,CustomPopupWindow.CustomPopupWindowListener{

    @BindView(R.id.home_vp)
    ViewPager homeVp;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.home_tab_Ll)
    HomeTabLayout homeTabLl;

    @Inject
    int[] tabIcons;
    @Inject
    String[] tabStrs;

    @Inject
    CustomPopupWindow grandmaPop;
    // viewpager
    HomeViewPagerAdapter adapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_home; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        List<OneLevelBaseFragment> fragments = new ArrayList<>();
        fragments.add(new LifeFragment());
        fragments.add(new BrandFragment());
        fragments.add(new ShoppingCardFragment());
        fragments.add(new MineFragment());
      adapter = new HomeViewPagerAdapter(getSupportFragmentManager(),fragments);
        homeVp.setAdapter(adapter);
        homeVp.setOffscreenPageLimit(fragments.size());
        homeTabLl.setTab(homeVp,tabStrs,tabIcons ,null);



        grandmaPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {

                grandmaPop.show();
                backgroundAlpha(0.7f);
            }
        });

    }

    @Override
    protected void onReceiveBack() {
        if(adapter!=null){
            for(OneLevelBaseFragment fragment:adapter.getFragments()){//回调fragment 网络状态发生改变
                fragment.onReceiveBack();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    public HomeActivity getActivity() {
        return this;
    }



    /**
     *
     * popwindow 统一管理
     */
    @Override
    public void initPopupView(View contentView) {
        contentView.findViewById(R.id.grandma_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        contentView.findViewById(R.id.pop_close_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                grandmaPop.dismiss();
            }
        });

    }
}
